﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using PriErp.Tools.Extensions;
using DevComponents.DotNetBar;
using System.Drawing;

namespace PriErp.Frm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            if (IsFirst("PriErp"))
            {
                //检查是否有新版本
                //Process theProcess = new Process();
                //theProcess.StartInfo = new ProcessStartInfo("AdvisorUpdate.exe");
                //theProcess.Start();
                //theProcess.WaitForExit();

                GlobalProvider.m_BaseStyle = AppSetting.GetConfig("Theme").ToEnum<eStyle>();
                GlobalProvider.sColor = Color.FromArgb(AppSetting.GetConfig("ColorTint").ToInt32());
                StyleManager.ChangeStyle(GlobalProvider.m_BaseStyle, GlobalProvider.sColor);
                LoginDlg dlg = new LoginDlg();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    MainFrm mFrm = new MainFrm();
                    Application.Run(mFrm);
                }
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ErrorReport dlg = new ErrorReport(e.Exception);
            dlg.ShowDialog();
        }

        /// <summary>
        /// 判断程序是否运行，不允许多开
        /// </summary>
        /// <returns>返回是否运行</returns>
        private static bool HasRunningProcess()
        {
            bool returnValue = false;
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id && process.MainModule.FileName ==
                       Assembly.GetExecutingAssembly().Location)
                {
                    returnValue = true;
                    break;
                }
            }
            return returnValue;
        }

        ///<summary> 
        ///判断程序是否正在运行 
        ///</summary> 
        ///<param name="appId">程序名称</param> 
        ///<returns>如果程序是第一次运行返回True,否则返回False</returns> 
        static bool IsFirst(string appId)
        {
            bool ret = false;
            if (OpenMutex(0x1F0001, 0, appId) == IntPtr.Zero)
            {
                CreateMutex(IntPtr.Zero, 0, appId);
                ret = true;
            }
            return ret;
        }

        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr OpenMutex(
            uint dwDesiredAccess, // access 
            int bInheritHandle,    // inheritance option 
            string lpName          // object name 
            );
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr CreateMutex(
            IntPtr lpMutexAttributes, // SD 
            int bInitialOwner,        // initial owner 
            string lpName             // object name 
            );
    }
}
